package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.IssueVo;
import com.cskaoyan.service.IssueService;
import com.cskaoyan.service.IssueServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;

@WebServlet("/admin/issue/*")
public class AdminIssueServlet extends WdBaseServlet {
    private IssueService issueService = new IssueServiceImpl();

    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String pageStr = request.getParameter("page");
        Integer page;
        if (pageStr != null && pageStr != "") {
            page = Integer.parseInt(pageStr);
        } else {
            page = null;
        }

        String limitStr = request.getParameter("limit");
        Integer limit;
        if (limitStr != null && limitStr != "") {
            limit = Integer.parseInt(limitStr);
        } else {
            limit = null;
        }
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");


        String question = request.getParameter("question");

        // 2. Service封装对象
        BaseRespVo baseRespVo = issueService.list(page, limit, question, sort, order);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String resJstr = request.getReader().readLine();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        ObjectMapper objectMapper = new ObjectMapper();
        MarketIssue marketIssue = objectMapper.readValue(resJstr, MarketIssue.class);

        // 2. Service封装对象
        BaseRespVo baseRespVo = issueService.create(marketIssue);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String resJstr = request.getReader().readLine();
        ObjectMapper objectMapper = new ObjectMapper();
        IssueVo issueVo = objectMapper.readValue(resJstr, IssueVo.class);

        // 2. Service封装对象
        BaseRespVo baseRespVo = issueService.update(issueVo);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String resJstr = request.getReader().readLine();
        ObjectMapper objectMapper = new ObjectMapper();
        IssueVo issueVo = objectMapper.readValue(resJstr, IssueVo.class);
        Integer id = issueVo.getId();

        // 2. Service封装对象
        BaseRespVo baseRespVo = issueService.delete(id);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;


    }
}
